﻿package com.shawnblais.ui
{
    import com.gskinner.motion.*;
    import fl.motion.easing.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.view.*;

    public class ContactForm3D extends BasicMenu3D
    {
        protected var _contactForm:MovieClip;
        protected var _aFields:Array;
        protected var _dFields:Dictionary;

        public function ContactForm3D(vselectedIndex:Viewport3D, selectedIndex:Number = -1, dselectedIndex:DisplayObject3D = null) : void
        {
            this._dFields = new Dictionary(true);
            this._aFields = [];
            super(vselectedIndex, selectedIndex, dselectedIndex);
            enableBackButton = true;
            return;
        }// end function

        protected function onTextOver(event:MouseEvent)
        {
            _viewport.buttonMode = true;
            _viewport.useHandCursor = true;
            return;
        }// end function

        protected function sendComplete(event:Event)
        {
            this._contactForm.content_mc.play();
            return;
        }// end function

        override protected function onBackClick(event:MouseEvent) : void
        {
            this._contactForm.content_mc.gotoAndStop(1);
            super.onBackClick(event);
            return;
        }// end function

        override public function setButtonListeners(param1:Boolean = true)
        {
            var add:* = param1;
            this._contactForm = _aMaterialMovies[0];
            this._contactForm.bg_mc.addEventListener(MouseEvent.ROLL_OVER, function ()
            {
                _viewport.buttonMode = false;
                _viewport.useHandCursor = false;
                return;
            }// end function
            );
            this._contactForm.content_mc.name_txt.addEventListener(MouseEvent.CLICK, this.focusText);
            this._contactForm.content_mc.name_txt.addEventListener(KeyboardEvent.KEY_DOWN, this.checkKeysDown);
            this._contactForm.content_mc.name_txt.addEventListener(MouseEvent.MOUSE_OVER, this.onTextOver);
            this._dFields[this._contactForm.content_mc.name_txt] = 0;
            this._aFields[0] = this._contactForm.content_mc.name_txt;
            this._contactForm.content_mc.email_txt.addEventListener(MouseEvent.CLICK, this.focusText);
            this._contactForm.content_mc.email_txt.addEventListener(KeyboardEvent.KEY_DOWN, this.checkKeysDown);
            this._contactForm.content_mc.email_txt.addEventListener(MouseEvent.MOUSE_OVER, this.onTextOver);
            this._dFields[this._contactForm.content_mc.email_txt] = 1;
            this._aFields[1] = this._contactForm.content_mc.email_txt;
            this._contactForm.content_mc.content_txt.addEventListener(MouseEvent.CLICK, this.focusText);
            this._contactForm.content_mc.content_txt.addEventListener(KeyboardEvent.KEY_DOWN, this.checkKeysDown);
            this._contactForm.content_mc.content_txt.addEventListener(MouseEvent.MOUSE_OVER, this.onTextOver);
            this._dFields[this._contactForm.content_mc.content_txt] = 2;
            this._aFields[2] = this._contactForm.content_mc.content_txt;
            this._contactForm.content_mc.sendButton_mc.mouseChildren = false;
            this._contactForm.content_mc.sendButton_mc.addEventListener(MouseEvent.MOUSE_UP, this.checkForm);
            this._contactForm.content_mc.sendButton_mc.addEventListener(MouseEvent.MOUSE_OVER, this.onSubmitOver);
            this._contactForm.content_mc.sendButton_mc.addEventListener(MouseEvent.MOUSE_OUT, this.onSubmitOut);
            this._contactForm.content_mc.name_error.visible = false;
            this._contactForm.content_mc.email_error.visible = false;
            this._contactForm.content_mc.content_error.visible = false;
            return;
        }// end function

        protected function checkForm(event:Event)
        {
            var _loc_2:Boolean = true;
            this._contactForm.content_mc.name_error.visible = false;
            this._contactForm.content_mc.email_error.visible = false;
            this._contactForm.content_mc.content_error.visible = false;
            if (this._contactForm.content_mc.name_txt.text.length < 2)
            {
                this._contactForm.content_mc.name_error.visible = true;
                _loc_2 = false;
            }
            if (this._contactForm.content_mc.email_txt.text.length < 5)
            {
                this._contactForm.content_mc.email_error.visible = true;
                _loc_2 = false;
            }
            if (this._contactForm.content_mc.content_txt.text.length < 5)
            {
                this._contactForm.content_mc.content_error.visible = true;
                _loc_2 = false;
            }
            if (_loc_2)
            {
                this.sendMail();
            }
            return;
        }// end function

        protected function checkKeysDown(event:KeyboardEvent) : void
        {
            var e:* = event;
            if (e.keyCode == 9)
            {
                setTimeout(function (param1)
            {
                if (_dFields[param1.target] == _aFields.length--)
                {
                    _viewport.containerSprite.stage.focus = _aFields[0];
                }
                else
                {
                    _viewport.containerSprite.stage.focus = _aFields[_dFields[e.target] + 1];
                }
                TextField(_viewport.containerSprite.stage.focus).setSelection(0, 0);
                return;
            }// end function
            , 1, e);
            }
            return;
        }// end function

        protected function onSubmitOver(event:Event):void
        {
            new GTween(event.target, 0.35, {alpha:2}, {ease:Cubic.easeOut});
            _viewport.buttonMode = true;
            _viewport.useHandCursor = true;
            return;
        }// end function

        protected function focusText(event:MouseEvent)
        {
            _viewport.containerSprite.stage.focus = TextField(event.target);
            event.target.setSelection(0, 0);
            return;
        }// end function

        protected function onSubmitOut(event:Event):void
        {
            new GTween(event.target, 0.35, {alpha:1}, {ease:Cubic.easeOut});
            return;
        }// end function

        protected function sendIOError(event:Event)
        {
            trace("IO Error");
            return;
        }// end function

        protected function sendMail()
        {
            var _loc_1:* = new URLVariables();
            _loc_1.name = this._contactForm.content_mc.name_txt.text;
            _loc_1.email = this._contactForm.content_mc.email_txt.text;
            _loc_1.content = this._contactForm.content_mc.content_txt.text;
            this._contactForm.content_mc.gotoAndStop(2);
            var _loc_2:* = new URLRequest("flashEmail.php");
            _loc_2.data = _loc_1;
            _loc_2.method = URLRequestMethod.POST;
            var _loc_3:* = new URLLoader();
            _loc_3.dataFormat = URLLoaderDataFormat.VARIABLES;
            _loc_3.addEventListener(Event.COMPLETE, this.sendComplete);
            _loc_3.addEventListener(IOErrorEvent.IO_ERROR, this.sendIOError);
            _loc_3.load(_loc_2);
            return;
        }// end function

    }
}
